﻿$(function() {

    addMoreLinkBehaviour();

    //SyntaxHighlighter.config.clipboardSwf = 'http://joelabrahamsson.com/syntaxhighlighter/scripts/clipboard.swf';
    SyntaxHighlighter.config.clipboardSwf = 'http://joelabrahamsson.com/syntaxhighlighter/scripts/clipboard.swf';
    SyntaxHighlighter.all();

    addTwitterMoreLink();

    animateNewComment();

});

function addMoreLinkBehaviour() {
    $('#.partialLoad #moreLink').live("click", function () {
        $(this).html("<img src='http://joelabrahamsson.com/images/ajax-loader.gif' />");
        $.get($(this).attr("href"), function (response) {
            $('.partialLoad ol').append($("ol", response).html());
            $('.partialLoad #moreLink').replaceWith($("#moreLink", response));
        });
        return false;

    });
}

function addTwitterMoreLink() {
    var twitterItemsCount = 6;
    if ($('#twitterMoreLink').length > 0)
        return;

    $('#twitterFeed').append('<a id="twitterMoreLink" href="javascript:void(0);">view more</a>');
    $('#twitterMoreLink').click(function() {
    //$(this).append("<img style='position: absolute;' src='http://joelabrahamsson.com/images/ajax-loader2.gif' />");
    $(this).append("<img style='position: absolute;' src='http://joelabrahamsson.com/images/ajax-loader2.gif' />");
        $.get("/twitterfeed?twitterItemsCount=" + twitterItemsCount, function(response) {
            $('#twitterFeed ol').append($("li", response));
            $('#twitterMoreLink').html("view more");
        });
        twitterItemsCount = twitterItemsCount + 3;
    });
};

function animateNewComment() {
    var re = new RegExp("#comment[0-9]+");
    if (location.hash != null && location.hash.match(re)) {
        var id = re.exec(location.hash) + '';
        $(id).hide();
        $(id).fadeIn(800);
    }
}

function initCommentPreview(previewUrl) {
    $('#comment').after("<a class='previewLink' id='previewLink' href='javascript:void(0);'>Preview</a>");
    $('#previewLink').click(function() {
        //$(this).append("<img style='position: absolute;' src='http://joelabrahamsson.com/images/ajax-loader2.gif' />");
    $(this).append("<img style='position: absolute;' src='http://joelabrahamsson.com/images/ajax-loader2.gif' />");
        $.post(previewUrl, { comment: $('#comment').val() }, showPreview);
    });

    $('#comment').after("<a class='previewLink' id='hidePreviewLink' href='javascript:void(0);'>Continue writing</a>");
    $('#hidePreviewLink').hide();
    $('#hidePreviewLink').click(hidePreview);
}

function showPreview(comment) {
    $('#comment').after("<div id='previewBox'>" + comment + "</div>");
    SyntaxHighlighter.highlight();
    $('#comment').hide();
    $('#previewLink').hide();
    $('#hidePreviewLink').show();
    $('#previewLink').html("Preview");
}

function hidePreview() {
    $('#hidePreviewLink').hide();
    $('#previewLink').show();
    $('#comment').show();
    $('#previewBox').remove();
}